<?php
class CustomClass {
	static function getFieldListsByModelId($model_id){
		return db_factory::query("SELECT * FROM `".TABLEPRE."witkey_custom_fields` where model_id = ".intval($model_id).' order by listorder asc ');
	}
	static function getExtCodeByModelId($model_id){
		$result = self::getFieldListsByModelId($model_id);
		$extCode = array();
		if($result){
			foreach ($result as $k=>$v){
				$extCode[]=$v['f_code'];
			}
		}
		return $extCode;
	}
	static function createExtData($objid,$model_id,$data){
		$extCode = CustomClass::getExtCodeByModelId($model_id);
		if($data){
			foreach ($data as $k=>$v){
				if(in_array($k, $extCode)){
					$fieldInfo = self::getFieldName($k);
					$extData = array();
					$extData[$k]['fieldname'] = $fieldInfo['f_name'];
					$extData[$k]['content'] = kekezu::escape($v);
					if($extData){
						$jsondata = serialize($extData);
						$tabObj = keke_table_class::get_instance("witkey_custom_fields_ext");
						$fields = array();
						$fields['c_id']=$fieldInfo['id'];
						$fields['model_id']=$model_id;
						$fields['extdata']=$jsondata;
						$fields['objid']=$objid;
						$tabObj->save($fields);
					}
				}
			}
		}
	}
	static function getExtData($objid,$model_id){
		return  db_factory::query("SELECT a.* FROM `".TABLEPRE."witkey_custom_fields_ext` a LEFT JOIN `".TABLEPRE."witkey_custom_fields` b ON  a.c_id = b.id WHERE a.model_id = ".intval($model_id)." and  a.objid = ".intval($objid).' order by b.listorder asc ');
	}
	static function getFieldName($fieldcode){
		if(!$fieldcode){
			return false;
		}
		return db_factory::get_one("SELECT * FROM `".TABLEPRE."witkey_custom_fields` where f_code = '".$fieldcode."'");
	}
	static function updateExtData($cid){
		$extInfo    = db_factory::get_one("SELECT * FROM `".TABLEPRE."witkey_custom_fields_ext` WHERE c_id = ".intval($cid));
		if($extInfo['extdata']){
			$oldData = unserialize($extInfo['extdata']);
			$fieldsInfo = db_factory::get_one("SELECT * FROM `".TABLEPRE."witkey_custom_fields` where id = ".intval($cid));
			if($fieldsInfo['f_code']){
				$oldData[$fieldsInfo['f_code']]['fieldname'] = $fieldsInfo['f_name'];
				db_factory::execute("UPDATE `".TABLEPRE."witkey_custom_fields_ext` SET `extdata`='".serialize($oldData)."' WHERE (`id`='".intval($extInfo['id'])."')");
			}
		}	
	}
	static function delExtData($cid){
		db_factory::execute("delete FROM `".TABLEPRE."witkey_custom_fields_ext` WHERE c_id = ".intval($cid));
	}
	static function delExtDataByObjId($objid,$model_id){
		db_factory::execute("delete FROM `".TABLEPRE."witkey_custom_fields_ext` WHERE objid = ".intval($objid)." and model_id = ".intval($model_id));
	}
}
?>